/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ac.ui.controls;

import ac.ui.custemItmes;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;

/**
 *
 * @author empcl_000
 */
public class PCLcomboBox extends JComboBox<Object> {

    ac.lib.database.operations db = new ac.lib.database.operations();
    private boolean required;

    public boolean isRequired() {
        return required;
    }

    public void setRequired(boolean required) {
        this.required = required;
    }

    private String type;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public  String getValue() {

        String returnData = "";
        try {
            ac.ui.controls.Item itemCate = (ac.ui.controls.Item) this.getSelectedItem();
            returnData = itemCate.getValue();
        } catch (Exception ex) {
        }

        return returnData;
    }

    public  String getText( ) {

        String returnData = "";
        try {
            ac.ui.controls.Item itemCate = (ac.ui.controls.Item) this.getSelectedItem();
            returnData = itemCate.getText();
        } catch (Exception ex) {
        }

        return returnData;
    }

    public  void setSelectonByValue(  String value) {

        for (int i = 0; i < this.getItemCount(); i++) {
            if (value != null) {
                ac.ui.controls.Item itemCate = (ac.ui.controls.Item) this.getItemAt(i);
///if (!value.equals("")){
                if (itemCate.getValue().equals(value)) {
                    this.setSelectedIndex(i);
                }
            }
            ///  }

        }

    }

    public void setFill( String table, String valueFiled, String TextFilde) {
        Vector model = new Vector();
        db.setOpenCon();
        ResultSet rs = db.getDataWithOutCon(table);
        try {
            while (rs.next()) {
                model.addElement(new Item(rs.getString(valueFiled), rs.getString(TextFilde)));
            }
        } catch (SQLException ex) {
            Logger.getLogger(custemItmes.class.getName()).log(Level.SEVERE, null, ex);
        }
        this.setModel(new DefaultComboBoxModel(model));
        db.setCloseCon();
    }

    public void setFill( String[] data) {
        Vector model = new Vector();
        for (String d : data) {
            model.addElement(new Item(d, d));
        }
        this.setModel(new DefaultComboBoxModel(model));

    }

    public void setFill( String[] value, String[] text) {
        Vector model = new Vector();
        for (int i = 0; i < text.length; i++) {
            model.addElement(new Item(value[i], text[i]));
        }
        this.setModel(new DefaultComboBoxModel(model));
    }


}
